﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataAccessTest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnMySql_Click(object sender, EventArgs e)
        {
            var db = Light.Data.DatabaseFactory.Create("Server=Server;Database=Database;Uid=Uid;Pwd=Pwd;", Light.Data.DatabaseFactory.SqlClientProvider);
            var dt = new DataTable("t_users");
            db.Fill("SELECT * FROM `t_users` ", dt);
            dt.Rows.Add(new object[] { 1, "user" });
            db.Update(dt);
            this.myGrid.DataSource = dt;
            myGrid.Tag = db;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            var db = myGrid.Tag as Light.Data.IDatabase;
            var dt = myGrid.DataSource as DataTable;
            if (db != null && dt != null)
            {
                db.Update(dt);
            }

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            var types = Properties.Settings.Default.dataType.Split(',');
            foreach (string item in types)
            {
                cmbType.Items.Add(item);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {

            try
            {
                var dtSource = new DataTable();
                var fileName = Application.StartupPath + "\\" + cmbType.Text+".config";
                System.IO.File.WriteAllText(fileName, "");
                var db = Light.Data.DatabaseFactory.Create("", cmbType.Text);

                db.Fill("", dtSource);
                myGrid.DataSource = dtSource;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var fileName = Application.StartupPath + "\\" + cmbType.Text + ".sql";
           
        }


    }
}
